<template>
    <el-row class="home" :gutter="20">
        <el-col :span="8" >
            <el-card shadow="hover" style="height: 320px; " class="user">
                <div class="user_head">管理</div>
                <div class="user_img">
                     <el-avatar :size="100" :src="userImg"></el-avatar>
                </div>
                <div class="user_info">
                    <span class="user_name">轩忆</span>
                </div>
            </el-card>
            <el-card shadow="hover" style="padding-bottom: 30px">
              <el-table :data="tableData">
                    <el-table-column show-overflow-tooltip v-for="(val, key) in tableLabel" :key="key" :prop="key" :label="val" ></el-table-column>
                </el-table>
            </el-card>
        </el-col>
        <el-col :span="16">
            <div class="num">
                <el-card shadow="hover" v-for="item in nbInfo" :key="item.name">
                    <div class="num_item">
                        <div><i :class="item.icon" ></i></div>
                        <div class="detail">
                            <span class="txt">{{item.name}}</span>
                            <span class="data">{{item.num}}</span>
                        </div>
                    </div>
                </el-card>
            </div>

            <el-card shadow="hover">
                <echart style="height:280px" :chartData="echartData.order"></echart>
            </el-card>

            <div class="graph">
                <el-card shadow="hover">
                    <echart style="height:260px" :chartData="echartData.user"></echart>
                </el-card>
                <el-card shadow="hover">
                    <echart style="height:260px" :chartData="echartData.category" :isAxisChart="false"></echart>
                </el-card>
            </div>
        </el-col>
    </el-row>
</template>

<script>
import Echart from '../../components/Echart'
export default {
  components: {
    Echart
  },
  data () {
    return {
      userImg: require('../../assets/images/profile.jpg'),
      countData: [],
      tableData: [],
      tableLabel: {
        name: '课程',
        todayBuy: '今日购买',
        monthBuy: '本月购买',
        totalBuy: '总购买'
      },
      // echart 数据
      echartData: {
        order: {
          xData: [],
          series: []
        },
        user: {
          xData: [],
          series: []
        },
        category: {
          series: []
        }
      },

      nbInfo: [
        {
          icon: 'el-icon-user icon',
          name: '用户',
          num: '37'
        },
        {
          icon: 'el-icon-shopping-cart-full icon',
          name: '商品',
          num: '537'
        },
        {
          icon: 'el-icon-notebook-1 icon',
          name: '订单',
          num: '259'
        }
      ]
    }
  },
  methods: {
    getTableData () {
      this.$http.get('/home/getData').then(
        res => {
          res = res.data
          this.tableData = res.data.tableData

          // 订单折线图
          const order = res.data.orderData
          this.echartData.order.xData = order.date
          // 1、取出series中的name部分--键名
          const keyArray = Object.keys(order.data[0])
          console.log(keyArray)
          keyArray.forEach(key => {
            this.echartData.order.series.push({
              name: key === 'wechat' ? '小程序' : key,
              data: order.data.map(item => item[key]),
              type: 'line'
            })
          })

          // 用户柱状图
          this.echartData.user.xData = res.data.userData.map(item => item.date)
          this.echartData.user.series.push({
            name: '新增用户',
            data: res.data.userData.map(item => item.new),
            type: 'bar'
          })
          this.echartData.user.series.push({
            name: '活跃用户',
            data: res.data.userData.map(item => item.active),
            type: 'bar',
            barGap: 0
          })

          // 视频饼图
          this.echartData.category.series.push({
            data: res.data.videoData,
            type: 'pie'
          })
        }
      )
    }
  },
  created () {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/scss/home.scss';
.el-avatar{
  border:1px solid #eee;
}

</style>
